﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.Services.Client;
using System.Threading;
using System.Windows.Threading;
using LoNumber.ServiceReference1;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Regions;

namespace LoNumber.View
{
    /// <summary>
    /// Interaction logic for LoNumberView.xaml
    /// </summary>
    public partial class LoNumberView : UserControl, IActivityView, IInputView
    {
        ServiceReference1.Container context = new ServiceReference1.Container(new Uri("http://localhost:1179/odata"));

        private InputPresenter _presenter;
        private ActivityPresenter presenter;
        private readonly IUnityContainer container;
        private readonly IRegionManager rManager;

        public LoNumberView(IUnityContainer c, IRegionManager r, ActivityPresenter presenter, InputPresenter presenter2)
        {
            InitializeComponent();

            container = c;
            rManager = r;

            this.presenter = presenter;
            presenter.View = this;

            _presenter = presenter2;
            _presenter.View = this;
        }
        public event EventHandler Result = delegate { };
        public string Number
        {
            get
            {
                return this.pm_LotNumberTextBox.Text.ToString();
            }
        }

        public string Number2
        {
            get
            {
                return this.pm_IdTextBox.Text.ToString();
            }
        }

        //private void Product_midDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{

        //    if (this.pm_Q_CCheckBox.IsChecked == true)
        //    {
        //        this.btnSuit.IsEnabled = true;

        //        this.btnNotSuit.IsEnabled = false;
        //    }

        //    else
        //    {
        //        this.btnSuit.IsEnabled = false;

        //        this.btnNotSuit.IsEnabled = true;
        //    }
        //}

        private void btnNotSuit_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("다시 품질검사를 신청하시겠습니까?", "부적합", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                if (this.pm_Q_CCheckBox.IsChecked == false)
                {
                    this.pm_Q_CCheckBox.IsChecked = true;
                    pm_DateDatePicker.SelectedDate = pm_DateDatePicker.SelectedDate.Value.AddDays(+7).Date;

                    this.btnSuit.IsEnabled = true;
                    this.btnNotSuit.IsEnabled = false;

                    Product_Mid mid = context.Product_Mid.Where(kkk => kkk.PM_Id == Int32.Parse(this.pm_IdTextBox.Text)).SingleOrDefault();
                    mid.PM_QC = this.pm_Q_CCheckBox.IsChecked.Value;
                    mid.PM_Date = this.pm_DateDatePicker.SelectedDate.Value.Date;

                    context.UpdateObject(mid);
                    context.SaveChanges();

                    //DataServiceCollection<Product_Mid> lstContext = new DataServiceCollection<Product_Mid>();

                    //lstContext.LoadCompleted += (objsender, eventArg) =>
                    //{
                    //    if (eventArg.Error == null)
                    //    {
                    //        var context3 = context.Product_Mid.Where(kkk => kkk.PM_LotNumber == this.TxtNumBinding.Text);

                    //        CollectionViewSource product_midViewSource = (CollectionViewSource)this.Resources["product_midViewSource"];
                    //        product_midViewSource.Source = context3;
                    //    }
                    //};

                    //lstContext.LoadAsync(context.Product_Mid);
                }
            }

            else
            {

            }
        }

        private void btnSuit_Click(object sender, RoutedEventArgs e)
        {
            Uri viewNav = new Uri("OutputView", UriKind.Relative);
            rManager.RequestNavigate(RegionNames.MainContentRegion, viewNav);

            Result(this, null);
        }


        public void SetLotNumber(string number)
        {
            this.TxtNumBinding.Text = number;

          
            //this.product_midDataGrid.DataContext = from kkk in context.Product_Mid
            //                                       where kkk.PM_LotNumber == number
            //                   
        }

        public void SetPmId(string number2)
        {
            var context2 = from kkk in context.Product_Mid
                           where kkk.PM_LotNumber == this.TxtNumBinding.Text
                           where kkk.PM_Id == Convert.ToInt32(number2)
                           select kkk;
            //context.Product_Mid.Where
            //(kkk => kkk.PM_LotNumber == this.TxtNumBinding.Text);

            this.product_midDataGrid.DataContext = context2;

            CollectionViewSource product_midViewSource = (CollectionViewSource)this.Resources["product_midViewSource"];
            product_midViewSource.Source = context2;

            if (this.pm_Q_CCheckBox.IsChecked == true)
            {
                this.btnSuit.IsEnabled = true;

                this.btnNotSuit.IsEnabled = false;
            }

            else
            {
                this.btnSuit.IsEnabled = false;

                this.btnNotSuit.IsEnabled = true;
            }

        }
    }
}
